package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class InengNewsAnalyse implements AnalyseNews{
	
	private static final String[] regex={
				"http://www.ineng.org/news/[0-9]*.html"};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}
			
			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}

			

			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}		
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();		
				if(!isDetailPage(url)){
				}				
				
				
				String title=null;
				String content=null;
				Long date=null;
				Document doc=Jsoup.parse(htmltxt);
				title=doc.select("h3.title").text();
				if (StringUtils.isBlank(title)) title=doc.select("h1").text();
				
				content=HtmlCleaner.getContentHtml(url,doc.select("div.myDisL.con-1"));
				if (StringUtils.isBlank(content)) {
					content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
				}
			
				if(doc.select("p.min_title em").size()> 1){
					date=DateUtils.matchDate(doc.select("p.min_title em").get(1).text());
				}
				if (date == null) {
					date=DateUtils.matchDate(doc.select("div.artInfo").text());
					if (date == null) {
						date=DateUtils.matchDate(doc.select("p").text());
					}
				}		
				
				if(StringUtils.isBlank(title)){
				}
				news.setUrl(url);		
				news.setTitle(title);
				news.setContent(content);
				news.setDate(date);
				return news;
			}
			
			public static void main(String[] args) {
				InengNewsAnalyse test=new InengNewsAnalyse();
			
				String url="http://www.ineng.org/news/100548";
				System.out.println(test.isDetailPage(url));
				UrlMeta meta=CrawlHTML.responseToURL(url);
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
